package com.ht.medicaltreatment.activity.haocai2

import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ConsumableSampleListAdapter
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.viewmodel.ConsumableSampleViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_consumable_sample_list_layout.cbAll
import kotlinx.android.synthetic.main.activity_consumable_sample_list_layout.recyclerView
import kotlinx.android.synthetic.main.activity_consumable_sample_list_layout.tvBinding
import kotlinx.android.synthetic.main.activity_consumable_sample_list_layout.tvSubmit

//耗材样本列表，样本盒绑定样本的界面
class ConsumableSampleListActivity : BaseActivity(), View.OnClickListener {

    override fun getLayoutId() = R.layout.activity_consumable_sample_list_layout

    private val model by viewModels<ConsumableSampleViewModel>()
    private var isOpen = false
    private lateinit var adapter:ConsumableSampleListAdapter
    private var nextPosition = -1
    private var sampleId = -1

    override fun init() {
        setTitle("样本")
        adapter = ConsumableSampleListAdapter()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        val id = intent.getIntExtra("id",0)
        ScanUtils.init()

        showProgress()
        model.requestList("$id"){
            dismissProgress()
            adapter.setNewInstance(it)
            cbAll.text = "全选：${it.size}"

            val item = it.filter { TextUtils.isEmpty(it.rfidCode) }
            if(item.size == 0){
                tvBinding.setBackgroundColor(getColor(R.color.color97))
                tvBinding.isEnabled = false
            }
        }

        tvBinding.setOnClickListener(this)
        tvSubmit.setOnClickListener(this)

        cbAll.setOnCheckedChangeListener { buttonView, isChecked ->
            adapter.data.forEach {
                if(TextUtils.isEmpty(it.rfidCode)){
                    it.isChecked = isChecked
                }
            }
            adapter.notifyDataSetChanged()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.close()
    }

    override fun onClick(v: View?) {
        when(v){
            tvBinding -> {
                if(isOpen){
                    tvBinding.text = "开始绑定"
                    ScanUtils.close()
                }else{
                    //val list = adapter.data.filter { it.isChecked }
                    val position = adapter.data.indexOfFirst { it.isChecked }
                    nextPosition = position
                    if(position >= 0){
                        tvBinding.text = "停止绑定"
                        sampleId = adapter.data[nextPosition].id
                        scan()
                    }
                }
                isOpen = !isOpen
            }
        }
    }


    private fun scan() {
        //绑定
        ScanUtils.open {rfid ->
            showProgress()
            LogUtils.i("绑定  ",rfid,sampleId)

            model.binding(rfid,"$sampleId",{
                dismissProgress()
                adapter.data[nextPosition].rfidCode = rfid
                adapter.notifyItemChanged(nextPosition)
                nextPosition++

                nextSampleId()

            },{
                dismissProgress()
                showToast(it)
            })
        }
    }

    fun nextSampleId(){
        if(nextPosition >= adapter.data.size){
            ScanUtils.close()
            showToast("全部绑定完毕")
            tvBinding.text = "开始绑定"
            isOpen = false
            tvBinding.setBackgroundColor(getColor(R.color.color97))
            tvBinding.isEnabled = false
            return
        }
        if(!adapter.data[nextPosition].isChecked){
            nextPosition++
            nextSampleId()
        }else{
            sampleId = adapter.data[nextPosition].id
        }
    }
}
